package de.baumann.browser.present.gg

import de.baumann.browser.BuildConfig
import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.gg.F
import de.baumann.browser.api.net.vo.gg.GGRule
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.gg.ICreateSmelterView
import de.baumann.browser.model.create
import de.baumann.browser.model.getPledgeRule
import de.baumann.browser.model.selectSameName
import de.baumann.browser.utils.web3j.Erc20Util
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.lang.Exception

class CreateSmelterPresenter : BasePresenter<ICreateSmelterView>() {

    fun createSmelterTransferToken(walletAddress: String, privateKey: String) {
        if (view.getSmelterName().isEmpty()) {
            view.onError("Smelter Name Is Empty")
            return
        }

        Observable.create(ObservableOnSubscribe<String> { emitter ->
            try {
                val hash = Erc20Util.transferERC20Token2(BuildConfig.WALLET_NODE_URL,
                        walletAddress,
                        view!!.getMortgageAmount(),
                        privateKey,
                        BuildConfig.GG_ADDRESS)
                if (hash == null) {
                    emitter.onError(Throwable("Transfer Failed"))
                } else {
                    emitter.onNext(hash)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                emitter.onError(e)
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<String> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(t: String) {
                        if (walletAddress.startsWith("odx")) {
                            createSmelter("0${walletAddress.substring(2)}", t)
                        } else {
                            createSmelter(walletAddress, t)
                        }
                    }

                    override fun onError(e: Throwable) {
                        view?.onError(e.message)
                    }
                })
    }

    fun createSmelter(address: String, hash: String) {
        create(view!!.getSmelterName(), address, hash)
                .subscribe(object : OdinObserver<Any>(view) {
                    override fun onSuccess(data: Any?) {
                        view?.transferSuccess()
                    }
                })
    }

    fun getRule() {
        getPledgeRule("1")
                .subscribe(object : OdinObserver<GGRule>(view) {
                    override fun onSuccess(data: GGRule?) {
                        if (data != null) {
                            view?.setRule(data.rule)
                        }
                    }
                })
    }

    fun checkName() {
        selectSameName(view!!.getSmelterName())
                .subscribe(object : OdinObserver<F>() {
                    override fun onSuccess(data: F?) {
                        if (data != null) {
                            view?.namePass(data.flag != 0)
                        }
                    }
                })
    }
}